package com.googlecode.gwtbb.client.only.widget.display.forum;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.SimplePanel;
import com.googlecode.gwtbb.client.only.glue.PathUtils;
import com.googlecode.gwtbb.client.shared.entity.forum.Forum;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Jul 16, 2008
 */
public class ForumLink extends Composite {
	private SimplePanel mainPanel;
	private Forum forum;

	public ForumLink(Forum forum) {
		mainPanel = new SimplePanel();
		initWidget(mainPanel);
		setForum(forum);
	}

	public Forum getForum() {
		return forum;
	}

	public void clear() {
		mainPanel.setWidget(null);
	}

	public void setForum(Forum forum) {
		this.forum = forum;
		mainPanel.setWidget(buildLink(forum));
	}

	private Hyperlink buildLink(Forum forum) {
		Hyperlink link = new Hyperlink();
		if (forum != null) {
			String text = forum.getName();
			link.setText(text);
			String target = PathUtils.link(PathUtils.concatToken(forum.getId(), text));
			link.setTargetHistoryToken(target);
		}
		return link;
	}
}